package sg.edu.nus.iss.ooad.jass.dao.file;

import java.util.ArrayList;
import java.util.List;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.dao.ICompanyDAO;
import sg.edu.nus.iss.ooad.jass.entity.Company;
import sg.edu.nus.iss.ooad.jass.entity.IJASSEntity;

/**
 *
 * Company Data Access Object for Properties File implementation.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : CompanyDAOPropertiesImpl.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class CompanyDAOPropertiesImpl extends AbstractPropertiesDAO implements ICompanyDAO {

        //Companys List
        public static Hashtable<String, IJASSEntity> companyCache = new Hashtable<String, IJASSEntity>();
        //Logger
        private final static Logger logger = Logger.getLogger(CompanyDAOPropertiesImpl.class.getName());
        //Properties filename
        public static final String COMPANY_FILE = "data.company.properties";
        //Variable to hold Properties for Company
        public static Properties properties = null;

        //Load at start-up
        static {
                logger.info("Start loading " + COMPANY_FILE);
                properties = loadProperties(COMPANY_FILE);
                if (properties != null) {
                        Company company = new Company();
                        companyCache = company.fromJASSPropertiesFormat(properties);
                }
                logger.info("Finish loading " + COMPANY_FILE);
        }

        /**
         * Constructor
         */
        public CompanyDAOPropertiesImpl() {
        }

        /**
         * Create new Company entity and store it to the persistent data.
         * @param company
         * @return
         */
        public synchronized boolean createCompany(Company company) {
                boolean retValue = false;

                //get number to total record
                int iTotalRecord = 0;
                String sTotalRecord = properties.getProperty("NumOfItems");
                if (sTotalRecord != null) {
                        iTotalRecord = Integer.parseInt(sTotalRecord);
                }
                iTotalRecord = iTotalRecord + 1;
                company.setID(String.valueOf(iTotalRecord));

                //update properties
                //COMPANY_ATTRIBUTES = {"ID", "Name", "CompanyAddress", "CompanyContactPerson", "CompanyContactNo", "CompanyEmail", "Visible"};
                properties.put("ID" + String.valueOf(iTotalRecord), company.getID());
                properties.put("Name" + String.valueOf(iTotalRecord), company.getName());
                properties.put("CompanyAddress" + String.valueOf(iTotalRecord), company.getAddress());
                properties.put("CompanyContactPerson" + String.valueOf(iTotalRecord), company.getContactPerson());
                properties.put("CompanyContactNo" + String.valueOf(iTotalRecord), company.getContactNo());
                properties.put("CompanyEmail" + String.valueOf(iTotalRecord), company.getEmail());
                properties.put("Visible" + String.valueOf(iTotalRecord), company.getVisible());
                properties.setProperty("NumOfItems", String.valueOf(iTotalRecord));

                //save it and update the cache
                storeProperties(properties, COMPANY_FILE);
                companyCache.put(company.getID(), company);

                return retValue;
        }

        /**
         * Retrieve an Company from persistent data.
         * @param ID
         * @return
         */
        public Company retrieveCompany(String ID) {
                Company retValue = null;

                retValue = (Company) companyCache.get(ID);

                return retValue;
        }

        /**
         * Update an Company from persistent data.
         * @param company
         * @return
         */
        public synchronized boolean updateCompany(Company company) {
                boolean retValue = false;

                if (companyCache.containsKey(company.getID())) {

                        //Transfer all existing job advertisement for this company
                        Company oldCompany = retrieveCompany(company.getID());
                        company.setJobadvertisement(oldCompany.getJobAdvertisement());

                        //update properties
                        properties.setProperty("Name" + company.getID(), company.getName());
                        properties.setProperty("CompanyAddress" + company.getID(), company.getAddress());
                        properties.setProperty("CompanyContactPerson" + company.getID(), company.getContactPerson());
                        properties.setProperty("CompanyContactNo" + company.getID(), company.getContactNo());
                        properties.setProperty("CompanyEmail" + company.getID(), company.getEmail());
                        properties.setProperty("Visible" + company.getID(), company.getVisible());

                        //save it and update the cache
                        storeProperties(properties, COMPANY_FILE);
                        companyCache.remove(company.getID());
                        companyCache.put(company.getID(), company);

                        //return value
                        retValue = true;
                }

                return retValue;
        }

        /**
         * Mark an Company as deleted from persistent data.
         * @param company
         * @return
         */
        public boolean deleteCompany(Company company) {
                throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         * Retrieve all Company from persistent data.
         * @return
         */
        public List<Company> getAllCompany() {
                List<Company> retValue = new ArrayList<Company>();

                //Looping the whole records from cache
                Enumeration<IJASSEntity> elements = companyCache.elements();
                while (elements.hasMoreElements()) {
                        Company company = (Company) elements.nextElement();
                        retValue.add(company);
                }

                return retValue;
        }

        /**
         * Cache existing Company from persistent data to the memory.
         * @return
         */
        public static synchronized Hashtable<String, IJASSEntity> cache() {
                return companyCache;
        }
}
